package com.winhxd.b2c.common.feign.order;

import com.winhxd.b2c.common.domain.order.condition.OrderRebateRecordCondition;
import com.winhxd.b2c.common.domain.order.model.OrderInfo;
import com.winhxd.b2c.common.domain.order.model.OrderRebateRecord;
import com.winhxd.b2c.common.domain.order.vo.OrderInfoVO;
import com.winhxd.b2c.common.domain.order.vo.OrderRebateRecordVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.constant.ServiceName;
import com.winhxd.b2c.common.domain.ResponseResult;

import feign.hystrix.FallbackFactory;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@FeignClient(value = ServiceName.ORDER_SERVICE, fallbackFactory = OrderServiceFallback.class)
public interface OrderServiceClient {

    @RequestMapping(value = "/order/4100/v1/orderClosedCalculateRevenue", method = RequestMethod.POST)
    ResponseResult<List<OrderRebateRecordVO>> orderClosedCalculateRevenue(@RequestParam("orderNo") String orderNo);

    @RequestMapping(value = "/order/4101/v1/getOrderInfoByOrderNo", method = RequestMethod.POST)
    ResponseResult<OrderInfoVO> getOrderInfoByOrderNo(@RequestParam("orderNo") String orderNo);

    @RequestMapping(value = "/order/4102/v1/getOrderRebateRecordVO", method = RequestMethod.POST)
    ResponseResult<List<OrderRebateRecord>> getOrderRebateRecordVO(@RequestParam("orderNo") String orderNo);

    /**
     * @Description:  修改订单实际收益
     * @Author: wangxiaoshun
     * @Date:  2019-01-15 10:27
     */
    @RequestMapping(value = "/order/4005/v1/modifyOrderRebateRecord", method = RequestMethod.POST)
    ResponseResult<OrderInfo> modifyOrderRebateRecord(OrderRebateRecordCondition condition);

}

@Component
class OrderServiceFallback implements OrderServiceClient, FallbackFactory<OrderServiceClient> {
    private static final Logger logger = LoggerFactory.getLogger(OrderServiceFallback.class);
    private Throwable throwable;

    public OrderServiceFallback() {
    }

    private OrderServiceFallback(Throwable throwable) {
        this.throwable = throwable;
    }

    @Override
    public OrderServiceClient create(Throwable throwable) {
        return new OrderServiceFallback(throwable);
    }


    @Override
    public ResponseResult<List<OrderRebateRecordVO>> orderClosedCalculateRevenue(String orderNo) {
        logger.error("OrderServiceClient -> orderClosedCalculateRevenue", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<OrderInfoVO> getOrderInfoByOrderNo(String orderNo) {
        logger.error("OrderServiceClient -> getOrderInfoByOrderNo", throwable);
        return new ResponseResult<OrderInfoVO>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<List<OrderRebateRecord>> getOrderRebateRecordVO(String orderNo) {
        logger.error("OrderServiceClient -> getOrderRebateRecordVO", throwable);
        return new ResponseResult<List<OrderRebateRecord>>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<OrderInfo> modifyOrderRebateRecord(OrderRebateRecordCondition condition) {
        logger.error("OrderServiceClient -> getOrderInfoByOrderNo", throwable);
        return new ResponseResult<OrderInfo>(BusinessCode.CODE_1001);
    }
}
